package very.util.quartz

import akka.actor.{ActorRef, ActorSelection}
import org.quartz.core.jmx.JobDataMapSupport
import org.quartz.{Job, JobDataMap, JobExecutionContext}

import scala.collection.JavaConverters._

class SimpleActorMessageJob extends Job {

  def execute(context: JobExecutionContext) {
    val dataMap = context.getJobDetail.getJobDataMap
    dataMap.get(SimpleActorMessageJob.Receiver) match {
      case v: ActorRef =>
        v ! dataMap.get(SimpleActorMessageJob.Message)
      case v: ActorSelection =>
        v ! dataMap.get(SimpleActorMessageJob.Message)
    }
  }
}
object SimpleActorMessageJob {
  val Receiver = "receiver"
  val Message = "message"

  def jobData(receiver: ActorRef, msg: AnyRef): JobDataMap = {
    JobDataMapSupport.newJobDataMap(
      Map(
        Receiver -> receiver,
        Message -> msg
      ).asJava
    )
  }

  def jobData(receiver: ActorSelection, msg: AnyRef): JobDataMap = {
    JobDataMapSupport.newJobDataMap(
      Map(
        Receiver -> receiver,
        Message -> msg
      ).asJava
    )
  }
}
